#ifndef __RT_THREAD_H__
#define __RT_THREAD_H__

#include <rtconfig.h>
#include <rtdebug.h>
#include <rtdef.h>
#include <rtservice.h>
#include <rtm.h>

#ifdef  __cplusplus
extern "C" {
#endif

/******************************这里是一些函数的声明,其具体的定义在某些c文件中********************************/
/**
 *  kernel object interface 
 */
void rt_system_object_init(void);   //这些函数的实现方式得看一下
// struct rt_object_information
// {
//     enum rt_object_class_type type;                 /*object class type*/
//     rt_list_t                 object_list;          /*object list*/
//     rt_size_t                 object_size;          /*object size*/
// };
struct rt_object_information * rt_object_get_information(enum rt_object_class_type  type);

// struct rt_object
// {
//     char name[RT_NAME_MAX];                      /*name of kernel object*/
//     rt_uint8_t type;                             /*type of kernel object*/
//     rt_uint8_t flag;                             /*flag of kernel object*/

// #ifdef RT_USING_MODULE
//     void    *module_id;                          /*id of application module*/
// #endif
//     rt_list_t list                               /*list node of kernel object*/  //里面就是两个指针
// };
void rt_object_init(struct rt_object  *object,enum rt_object_class_type type,const char *name);

void rt_object_detach(rt_object_t object);  //干嘛用的

//注意这里是返回指针对象
rt_object_t rt_object_allocate(enum rt_object_class_type type,const char *name);

void rt_object_delete(rt_object_t object);  //object delete

rt_bool_t rt_object_is_systemobject(rt_object_t object);

rt_object_t rt_object_find(const char *name,rt_uint8_t type);

#ifdef  RT_USING_HOOK
void rt_object_attach_sethook(void (*hook)(struct rt_object *object));   //参数是一个函数指针(函数的参数类型为......)
void rt_object_detach_sethook(void (*hook)(struct rt_object *object));
void rt_object_trytake_sethook(void (*hook)(struct rt_object *object));  //trytake 尝试
void rt_object_take_sethook(void (*hook)(struct rt_object *object));     //
void rt_object_put_sethook(void (*hook)(struct rt_object *object));
#endif


/**
 * clock & timer interface
 */
void rt_system_tick_init(void);
rt_tick_t rt_tick_get(void);  //uint32_t
void rt_tick_set(rt_tick_t tick);
void rt_tick_increase(void);
int rt_tick_from_millisecond(rt_int32_t ms);

void rt_system_timer_init(void);
void rt_system_timer_thread_init(void);

void rt_timer_init(rt_timer_t timer,const char *name,void (*timeout)(void *parameter),void *parameter,rt_tick_t time,rt_uint8_t flag);
rt_err_t rt_timer_detach(rt_timer_t timer);

// struct rt_timer
// {
//     struct rt_object parent;                        /*inherit from rt_object*/

//     rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];        //双向链表数组

//     void (*timeout_func)(void *parameter)           /*timeout function*/
//     void *parameter;                                /*timeout function's parameter*/

//     rt_tick_t   init_tick;                          /*timer timeout tick*/
//     rt_tick_t   timeout_tick;                       /*timeout tick*/   //这两个有什么意思
// };
// typedef struct rt_timer * rt_timer_t;               //定义一个指针变量

rt_timer_t rt_timer_create(const char *name,void (*timeout)(void *parameter),void *parameter,rt_tick_t time,rt_uint8_t flag);

rt_err_t rt_timer_delete(rt_timer_t timer);
rt_err_t rt_timer_start(rt_timer_t timer);
rt_err_t rt_timer_stop(rt_timer_t timer);
rt_err_t rt_timer_control(rt_timer_t timer,int cmd,void *arg);

rt_tick_t rt_timer_next_timeout_tick(void);
void rt_timer_check(void);

#ifdef RT_USING_HOOK
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer));
#endif

/**
 * thread interface   搞完还得整体性的看一看
 */
rt_err_t rt_thread_init(struct rt_thread *thread,
                                const char *name,
                                void (*entry)(void *parameter),
                                void *parameter,
                                void *stack_start,
                                rt_uint32_t stack_size,
                                rt_uint8_t priority,
                                rt_uint32_t tick
                                );
rt_err_t rt_thread_detach(rt_thread_t thread);   //参数是一个指针
rt_thread_t rt_thread_create(const char *name,   //返回的也是一个指针
                             void (*entry)(void *parameter),
                             void *parameter,
                             rt_uint32_t stack_size,
                             rt_uint8_t priority,
                             rt_uint32_t tick
                             );
rt_thread_t rt_thread_self(void);       //这些函数是怎样玩的,还得细究
rt_thread_t rt_thread_find(char * name);
rt_err_t rt_thread_startup(rt_thread_t thread);  //rt_err_t long 有符号数
rt_err_t rt_thread_delete(rt_thread_t thread);    //和函数rt_thread_detach相对应

rt_err_t rt_thread_yield(void);     //让出(同优先级的时间片?  读源码???)
rt_err_t rt_thread_delay(rt_tick_t tick);  //
rt_err_t rt_thread_control(rt_thread_t thread,int cmd,void *arg);
rt_err_t rt_thread_suspend(rt_thread_t thread);
rt_err_t rt_thread_resume(rt_thread_t thread);
void rt_thread_timeout(void *parameter);

#ifdef RT_USING_SIGNALS
void rt_thread_alloc_sig(rt_thread_t tid);  
void rt_thread_free_sig(rt_thread_t tid);
int rt_thread_kill(rt_thread_t tid,int sig);   //刨析其里子
#endif

#ifdef RT_USING_HOOK
void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread));  //干嘛的
void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread)); 
void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread)); 
#endif

/**   
 * idle thread interface   
 */
void rt_thread_idle_init(void);
#if defined(RT_USING_HOOK) || defined(RT_USING_IDLE_HOOK)
void rt_thread_idle_sethook(void (*hook)(void));
#endif
void rt_thread_idle_excute(void);
rt_thread_t rt_thread_idle_gethandler(void);

/**
 * schedule service  //调度器服务
 */
void rt_system_scheduler_init(void);  //
void rt_system_scheduler_start(void);

void rt_schedule(void);
void rt_schedule_insert_thread(struct rt_thread *thread);
void rt_schedule_remove_thread(struct rt_thread *thread);

void rt_enter_critical(void);  //调度器锁(但中断还是会发生)
void rt_exit_critical(void);
rt_uint16_t rt_critical_level(void);   //干嘛的？？

#ifdef RT_USING_HOOK
void rt_scheduler_sethook(void (*hook)(rt_thread_t from,rt_thread_t to));  //这里的有些写法很有意思
#endif

#ifdef RT_USING_SIGNALS
void rt_signal_mask(int signo);
void rt_signal_unmask(int signo);
rt_sighandler_t rt_signal_install(int signo,rt_sighandler_t handler);
int rt_signal_wait(const rt_sigset_t *set,rt_siginfo_t *si,rt_int32_t timeout);

int rt_system_signal_init(void);
#endif

/**
 * memory management interface
 */
#ifdef RT_USING_MEMPOOL
/**
 * memory pool interface    //内存池接口   淡薄明志,宁静致远
 */
rt_err_t rt_mp_init(struct rt_mempool *mp,
                    const char *name,
                    void *start,
                    rt_size_t size,
                    rt_size_t block_size);
rt_err_t rt_mp_detach(struct rt_mempool *mp);  //上面是针对静态资源的
rt_mp_t rt_mp_create(const char * name,
                    rt_size_t block_count,
                    rt_size_t block_size);
rt_err_t rt_mp_delete(rt_mp_t mp);   //typedef struct rt_mempool *  rt_mp_t;                //指针
/*先创建内存块  后分配???*/
void *rt_mp_alloc(rt_mp_t mp,rt_int32_t time);  //rt_int32_t     signed long
void rt_mp_free(void *block);   

#ifdef RT_USING_HOOK
void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp,void *block));
void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp,void *block));
#endif

#endif


#ifdef RT_USING_HEAP
/**
 * heap system interface  野火的最后一章得看(反复看)
 */
void rt_system_heap_init(void *begin_addr,void * end_addr);

void * rt_malloc(rt_size_t nbytes);
void rt_free(void *ptr); 
void * rt_realloc(void *ptr,rt_size_t nbytes);  //unsigned long 
void * rt_calloc(rt_size_t count,rt_size_t size);  
void * rt_malloc_align(rt_size_t size,rt_size_t align);
void rt_free_align(void *ptr);

void rt_memory_info(rt_uint32_t *total,
                    rt_uint32_t *used,
                    rt_uint32_t *max_used);
#ifdef  RT_USING_SLAB
void *rt_page_alloc(rt_size_t npages);
void rt_page_free(void *addr,rt_size_t npages);
#endif

#ifdef RT_USING_HOOK
void rt_malloc_sethook(void (*hook)(void *ptr,rt_uint32_t size));
void rt_free_sethook(void (*hook)(void *ptr));
#endif

#endif

#ifdef RT_USING_MEMHEAP
/**
 * memory heap object interface  和上面的东东有什么区别
 */
rt_err_t rt_memheap_init(struct rt_memheap *memheap,
                         const char *name,
                         void *start_addr,
                         rt_uint32_t size);
rt_err_t rt_memheap_detach(struct rt_memheap *heap);
void *rt_memheap_alloc(struct rt_memheap *heap,rt_uint32_t size);
void *rt_memheap_realloc(struct rt_memheap *heap,void *ptr,rt_size_t newsize);
void rt_memheap_free(void *ptr);   //觉得很难,但你得明白,这样才有前(钱)途!
#endif


/**
 * IPC
 */

#ifdef RT_USING_SEMAPHORE
/**
 * semaphore interface
 */
rt_err_t rt_sem_init(rt_sem_t sem,
                    const char *name,
                    rt_uint32_t value,
                    rt_uint8_t flag);
rt_err_t rt_sem_detach(rt_sem_t sem);  //重点还是在.c文件中
rt_sem_t rt_sem_create(const char *name,rt_uint32_t value,rt_uint8_t flag);
rt_err_t rt_sem_delete(rt_sem_t sem);

rt_err_t rt_sem_take(rt_sem_t sem,rt_int32_t time);  //这里是有符号数
rt_err_t rt_sem_trytake(rt_sem_t sem);
rt_err_t rt_sem_release(rt_sem_t sem);
rt_err_t rt_sem_control(rt_sem_t sem,int cmd,void *arg);
#endif

#ifdef RT_USING_MUTEX
/**
 * mutex interface
 */
rt_err_t rt_mutex_init(rt_mutex_t mutex,const char *name,rt_uint8_t flag);
rt_err_t rt_mutex_detach(rt_mutex_t mutex);
rt_mutex_t rt_mutex_create(const char *name,rt_uint8_t flag);
rt_err_t rt_mutex_delete(rt_mutex_t mutex);

rt_err_t rt_mutex_take(rt_mutex_t mutex,rt_int32_t time);
rt_err_t rt_mutex_release(rt_mutex_t mutex);
rt_err_t rt_mutex_control(rt_mutex_t mutex,int cmd,void *arg);
#endif

#ifdef RT_USING_EVENT
/**
 * event interface
 */
rt_err_t rt_event_init(rt_event_t event,const char *name,rt_uint8_t flag);
rt_err_t rt_event_detach(rt_event_t event);
rt_event_t rt_event_create(const char *name,rt_uint8_t flag);
rt_err_t rt_event_delete(rt_event_t event);

rt_err_t rt_event_send(rt_event_t event,rt_uint32_t set);
rt_err_t rt_event_recv(rt_event_t event,
                        rt_uint32_t set,
                        rt_uint8_t opt,
                        rt_int32_t timeout,
                        rt_uint32_t *recved);
rt_err_t rt_event_control(rt_event_t event,int cmd,void *arg);            
#endif

#ifdef RT_USING_MAILBOX
/**
 * mailbox interface
 */
rt_err_t rt_mb_init(rt_mailbox_t mb,
                    const char *name,
                    void *msgpool,
                    rt_size_t size,
                    rt_uint8_t flag);
rt_err_t rt_mb_detach(rt_mailbox_t mb);  //long
rt_mailbox_t rt_mb_create(const char *name,rt_size_t size,rt_uint8_t flag);
rt_err_t rt_mb_delete(rt_mailbox_t mb);

rt_err_t rt_mb_send(rt_mailbox_t mb,rt_uint32_t value);
rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
                         rt_uint32_t value,
                         rt_int32_t timeout);
rt_err_t rt_mb_recv(rt_mailbox_t mb,rt_uint32_t *value,rt_int32_t timeout);
rt_err_t rt_mb_control(rt_mailbox_t mb,int cmd,void *arg);
#endif

#ifdef RT_USING_MESSAGEQUEUE
/**
 * message queue interface
 */
rt_err_t rt_mq_init(rt_mq_t mq,
                    const char *name,
                    void *msgpool,
                    rt_size_t msg_size,
                    rt_size_t pool_size,
                    rt_uint8_t flag);
rt_err_t rt_mq_detach(rt_mq_t mq);  //
rt_mq_t rt_mq_create(const char *name,
                     rt_size_t msg_size,
                     rt_size_t max_msgs,
                     rt_uint8_t flag);
rt_err_t rt_mq_delete(rt_mq_t mq);

rt_err_t rt_mq_send(rt_mq_t mq,void *buffer,rt_size_t size);
rt_err_t rt_mq_urgent(rt_mq_t mq,void *buffer,rt_size_t size);
rt_err_t rt_mq_recv(rt_mq_t mq,
                    void *buffer,
                    rt_size_t size,
                    rt_int32_t timeout);
rt_err_t rt_mq_control(rt_mq_t mq,int cmd,void *arg);
#endif

#ifdef RT_USING_DEVICE
/**
 * device (I/O) system interface
 */
rt_device_t rt_device_find(const char *name);

rt_err_t rt_device_register(rt_device_t dev,
                            const char *name,
                            rt_uint16_t flags);
rt_err_t rt_device_unregister(rt_device_t dev);

rt_device_t rt_device_create(int type,int attach_size);
void rt_device_destroy(rt_device_t device);

rt_err_t rt_device_init_all(void);

rt_err_t rt_device_set_rx_indicate(rt_device_t dev,
                                   rt_err_t (*rx_ind)(rt_device_t dev,rt_size_t size));
rt_err_t rt_device_set_tx_complete(rt_device_t dev,
                                    rt_err_t (*tx_done)(rt_device_t dev,void *buffer));
rt_err_t rt_device_init(rt_device_t dev);
rt_err_t rt_device_open(rt_device_t dev,rt_uint16_t oflag);
rt_err_t rt_device_close(rt_device_t dev);
rt_size_t rt_device_read(rt_device_t dev,
                         rt_off_t pos,
                         void *buffer,
                         rt_size_t size);
rt_size_t rt_device_write(rt_device_t dev,
                          rt_off_t pos,
                          const void *buffer,
                          rt_size_t size);
rt_err_t rt_device_control(rt_device_t dev,int cmd,void *arg);
#endif

#ifdef RT_USING_MODULE
/**
 * module interface
 */
rt_module_t rt_module_load(const char *name,void *module_ptr);
rt_err_t rt_module_unload(rt_module_t module);
#ifdef RT_USING_DFS
rt_module_t rt_module_open(const char *filename);
rt_module_t rt_module_exec_cmd(const char *path,const char *cmd_line,int size);
#endif
void *rt_module_malloc(rt_size_t size);
void *rt_module_realloc(void *ptr,rt_size_t size);
void rt_module_free(rt_module_t module,void *addr);
rt_module_t rt_module_self(void);
rt_module_t rt_module_find(const char *name);

#ifdef RT_USING_HOOK
void rt_module_load_sethook(void (*hook)(rt_module_t module));
void rt_module_unload_sethook(void (*hook)(rt_module_t module));
#endif

void rt_module_init_object_container(struct rt_module *module);
rt_err_t rt_module_destroy(rt_module_t module);

/**
 * application module system initialization
 */
int rt_system_module_init(void);

#endif

/**
 * interrupt service
 */
/**
 *  rt_interrupt_enter and rt_interrupt_leave only can be called by BSP
 */
void rt_interrupt_enter(void);
void rt_interrupt_leave(void);

/**
 * the number of nested interrupts   //嵌套中断的数量
 */
rt_uint8_t rt_interrupt_get_nest(void);  //录音留证据

#ifdef RT_USING_HOOK
void rt_interrupt_enter_sethook(void (*hook)(void));
void rt_interrupt_leave_sethook(void (*hook)(void));
#endif

#ifdef RT_USING_COMPONENTS_INIT
void rt_components_init(void);
void rt_components_board_init(void);
#endif

/**
 * general kernel service
 */
#ifndef RT_USING_CONSOLE
#define rt_kprintf(...)
#define rt_kputs(str)
#else
void rt_kprintf(const char *fmt,...);
void rt_kputs(const char *str);
#endif

/**
 * 夫学须静也,才须学也。非学无以广才,非静无以成学!
 */

rt_int32_t rt_vsprintf(char *dest,const char *format,va_list arg_ptr);  //这里的va_list是干嘛的？？？
rt_int32_t rt_vsnprintf(char *buf,rt_size_t size,const char *fmt,va_list args);
rt_int32_t rt_sprintf(char *buf,const char *format,...);
rt_int32_t rt_snprintf(char *buf,rt_size_t size,const char *format,...);

#if defined(RT_USING_DEVICE)&&defined(RT_USING_CONSOLE)
rt_device_t rt_console_set_device(const char *name);
rt_device_t rt_console_get_device(void);
#endif

rt_err_t rt_get_errno(void);
void rt_set_errno(rt_err_t no);
int *_rt_errno(void);

#if !defined(RT_USING_NEWLIB) && !defined(_WIN32)   //两个都没有定义才可以
#ifndef errno
#define errno   *_rt_errno()        //用errno代替*_rt_errno() 
#endif
#endif

int __rt_ffs(int value);

void *rt_memset(void *src,int c,rt_ubase_t n);
void *rt_memcpy(void *dest,const void *src,rt_ubase_t n);

rt_int32_t rt_strncmp(const char *cs,const char *ct,rt_ubase_t count);
rt_int32_t rt_strcmp(const char *cs,const char *ct);
rt_size_t rt_strlen(const char *src);
char *rt_strdup(const char *s);

char *rt_strstr(const char *str1,const char *str2);
rt_int32_t rt_sscanf(const char *buf,const char *fmt,...);
char *rt_strncpy(char *dest,const char *src,rt_ubase_t n);
void *rt_memmove(void *dest,const void *src,rt_ubase_t n);
rt_int32_t rt_memcmp(const void *cs,const void *ct,rt_ubase_t count);
rt_uint32_t rt_strcasecmp(const char *a,const char *b);

void rt_show_version(void);

#ifdef RT_DEBUG
extern void (*rt_assert_hook)(const char *ex,const char *func,rt_size_t line);
void rt_assert_set_hook(void (*hook)(const char *ex,const char *func,rt_size_t line));

void rt_assert_handler(const char *ex,const char *func,rt_size_t line);
#endif 

#ifdef RT_USING_FINSH
#include <finsh_api.h>
#endif

#ifdef __cplusplus  //C
}
#endif

#endif